package com.bistu.xiong.factory.data;

import android.support.annotation.StringRes;

import com.bistu.xiong.factory.Factory;
import com.bistu.xiong.factory.R;
import com.bistu.xiong.factory.model.api.RspModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by XiongXiaoJun on 2017/12/17.
 */

public class RspCallback<T> implements Callback<RspModel<T>> {
    final DataSource.Callback<T> callback;

    public RspCallback(DataSource.Callback<T> callback) {
        this.callback = callback;
    }

    @Override
    public void onResponse(Call<RspModel<T>> call, Response<RspModel<T>> response) {
        //如果调用成功
        RspModel<T> rspModel = response.body();
        if(rspModel!=null && rspModel.success()){
            callback.onDataLoaded(rspModel.getData());
        }else{
            Factory.decodeRspCode(rspModel,callback);
        }
    }

    @Override
    public void onFailure(Call<RspModel<T>> call, Throwable t) {
        callback.onDataNotAvailable(R.string.data_network_error);
    }
}
